package com.weds.data_sync.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.weds.data_sync.mapper.DataSyncDbInfoMapper;
import com.weds.data_sync.pojo.dto.DataSyncDbInfoDTO;
import com.weds.data_sync.pojo.dto.PageDto;
import com.weds.data_sync.pojo.entity.DataSyncDbInfoDO;
import com.weds.data_sync.service.DbInfoService;
import com.weds.data_sync.util.CommonUtil;
import com.weds.data_sync.util.JsonData;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Objects;

/**
 * <p>
 * 数据同步-数据库信息 服务实现类
 * </p>
 *
 * @author lhw
 * @since 2023-12-29
 */
@Slf4j
@Service
public class DbInfoServiceImp extends ServiceImpl<DataSyncDbInfoMapper, DataSyncDbInfoDO> implements DbInfoService {

    @Autowired
    private DataSyncDbInfoMapper dbInfoMapper;

    @Override
    public Integer add(DataSyncDbInfoDTO dataSyncDbInfoDTO) {
        DataSyncDbInfoDO targetDO = new DataSyncDbInfoDO();
        BeanUtils.copyProperties(dataSyncDbInfoDTO, targetDO);
        return dbInfoMapper.insert(targetDO);
    }

    @Override
    public Integer del(List<Long> ids) {
        if (Objects.isNull(ids) || ids.isEmpty()) {
            return 0;
        }
        return dbInfoMapper.deleteBatchIds(ids);
    }

    @Override
    public Integer update(DataSyncDbInfoDTO dataSyncDbInfoDTO) {
        if (Objects.isNull(dataSyncDbInfoDTO.getId())) {
            log.info("修改数据库信息的id不能为空：{}", dataSyncDbInfoDTO);
            return 0;
        }
        DataSyncDbInfoDO dataSyncDbInfoDO = new DataSyncDbInfoDO();
        BeanUtils.copyProperties(dataSyncDbInfoDTO, dataSyncDbInfoDO);
        return dbInfoMapper.updateById(dataSyncDbInfoDO);
    }

    public JsonData.PageData<DataSyncDbInfoDO> queryData(PageDto pageDto) {
        Page<DataSyncDbInfoDO> pageInfo = new Page<>(pageDto.getPage(), pageDto.getSize());
        dbInfoMapper.selectPage(pageInfo, new QueryWrapper<DataSyncDbInfoDO>()
                .orderByAsc("id"));
        return CommonUtil.pageToDto(pageInfo);
    }

}
